﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Collections.Generic;

public partial class System_UserRoles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ActionTitle.Text = Yipusoft.Account.BLL.User.GetUser(Request.QueryString["UserID"]).UserName + "角色设置";
            BindRoleList();
            BindUserRoleList();
        }
    }
    private void BindRoleList()
    {
        RoleList.Items.Clear();
        List<RoleInfo> roles = Role.GetList("");
        List<RoleInfo> userRoles = Yipusoft.Account.BLL.User.GetRoleList(Request.QueryString["UserID"]);
        ListItem li;
        foreach (RoleInfo role in roles)
        {
            if (!userRoles.Contains(role))
            {
                li = new ListItem();
                li.Text = role.RoleName;
                li.Value = role.RoleID;
                RoleList.Items.Add(li);
            }
        }
    }
    private void BindUserRoleList()
    {
        UserRoleList.Items.Clear();
        List<RoleInfo> roles = Yipusoft.Account.BLL.User.GetRoleList(Request.QueryString["UserID"]);
        ListItem li;
        foreach (RoleInfo role in roles)
        {
            li = new ListItem();
            li.Text = role.RoleName;
            li.Value = role.RoleID;
            UserRoleList.Items.Add(li);
        }
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        if (RoleList.SelectedValue != "")
        {
            foreach (ListItem li in RoleList.Items)
            {
                if (li.Selected)
                    Yipusoft.Account.BLL.User.AddRole(Request.QueryString["UserID"], li.Value);
            }
            BindRoleList();
            BindUserRoleList();
        }
        else
            Prompt.Text = "警告：请选择一个权限！";
    }
    protected void ButtonRemove_Click(object sender, EventArgs e)
    {
        if (UserRoleList.SelectedValue != "")
        {
            foreach (ListItem li in UserRoleList.Items)
            {
                if (li.Selected)
                    Yipusoft.Account.BLL.User.RemoveRole(Request.QueryString["UserID"], li.Value);
            }
            BindRoleList();
            BindUserRoleList();
        }
        else
            Prompt.Text = "警告：请选择一个权限！";
    }
}
